package com.victor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.ShopUserLogin;
import com.victor.entity.finefood.Food;
import com.victor.entity.happy.Happy;
import com.victor.entity.hotel.Hotel;
import com.victor.service.PartnersService;
import com.victor.service.ShopUserLoginService;
import com.victor.utils.Msg;

/**
 * 商家登录
 * 
 * @author zhangbin
 *
 */
@RestController
public class ShopUserLoginController {

	@Autowired
	private ShopUserLoginService service;

	@Autowired
	private PartnersService partnersService;

	// 商家登录
	@RequestMapping("/shopuserlogin")
	public Msg shopuserlogin(String account, String password) {
		ShopUserLogin shopUserLogin = service.shopuserlogin(account);

		if (shopUserLogin != null) {
			// 判断密码
			if (password.equals(shopUserLogin.getPassword())) {
				// 通过shoptype查询shopid对应的商家
				if (shopUserLogin.getShoptype().equals("1")) {// 酒店商家
					Hotel hotel = partnersService.getHotelXQ(shopUserLogin.getShopid());
					return Msg.success().add("hotel", hotel);
				} else if (shopUserLogin.getShoptype().equals("2")) {// 美食商家
					Food food = partnersService.getFoodXQ(shopUserLogin.getShopid());
					return Msg.success().add("food", food);
				} else if (shopUserLogin.getShoptype().equals("3")) {// 娱乐商家
					Happy happy = partnersService.getHappyXQ(shopUserLogin.getShopid());
					return Msg.success().add("happy", happy);
				} else {
					return Msg.fail("shoptype错误");
				}
			}
			return Msg.fail("密码错误");
		}
		return Msg.fail("不存在该商家");

	}

	// 修改密码(传商家id和旧密码,新密码)
	@RequestMapping("/updateShopUserPassword")
	public Msg updateShopUserPassword(String shopid, String password, String newpassword) {
		ShopUserLogin shopUserLogin = service.shopuserloginByShopid(shopid);
		if (shopUserLogin != null) {
			if (password.equals(shopUserLogin.getPassword())) {
				boolean updResult = service.updateShopUserPassword(shopid, newpassword);
				if (updResult) {
					return Msg.success();
				}
				return Msg.fail();
			}
			return Msg.fail("原密码错误");
		}
		return Msg.fail("未查询到该商家id对应的商家");
	}
}
